<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Not Quite Not | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/not-quite-not.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/not-quite-not.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/not-quite-not.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/not-quite-not.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="controlling-relevance.html">控制相关度</a></span>
»
<span class="breadcrumb-node">Not Quite Not</span>
</div>
<div class="navheader">
<span class="prev">
<a href="query-scoring.html">« 使用查询结构修改相关度</a>
</span>
<span class="next">
<a href="ignoring-tfidf.html">忽略 TF/IDF »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="not-quite-not"></a>Not Quite Not<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/30_Not_quite_not.asciidoc">edit</a>
</h2>
</div></div></div>
<p>在互联网上搜索 “Apple”，返回的结果很可能是一个公司、水果和各种食谱。我们可以在 <code class="literal">bool</code> 查询中用 <code class="literal">must_not</code> 语句来排除像 <code class="literal">pie</code> 、 <code class="literal">tart</code> 、 <code class="literal">crumble</code> 和 <code class="literal">tree</code> 这样的词，从而将查询结果的范围缩小至只返回与 “Apple” （苹果）公司相关的结果：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "text": "apple"
        }
      },
      "must_not": {
        "match": {
          "text": "pie tart fruit crumble tree"
        }
      }
    }
  }
}</pre>
</div>
<p>但谁又敢保证在排除 <code class="literal">tree</code> 或 <code class="literal">crumble</code> 这种词后，不会错失一个与苹果公司特别相关的文档呢？有时， <code class="literal">must_not</code> 条件会过于严格。</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="boosting-query"></a>权重提升查询<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/30_Not_quite_not.asciidoc">edit</a>
</h3>
</div></div></div>
<p><a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-boosting-query.html" class="ulink" target="_top"><code class="literal">boosting</code> 查询</a>
恰恰能解决这个问题。它仍然允许我们将关于水果或甜点的结果包括到结果中，但是使它们降级——即降低它们原来可能应有的排名：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /_search
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "text": "apple"
        }
      },
      "negative": {
        "match": {
          "text": "pie tart fruit crumble tree"
        }
      },
      "negative_boost": 0.5
    }
  }
}</pre>
</div>
<p>它接受 <code class="literal">positive</code> 和 <code class="literal">negative</code> 查询。只有那些匹配 <code class="literal">positive</code> 查询的文档罗列出来，对于那些同时还匹配 <code class="literal">negative</code> 查询的文档将通过文档的原始 <code class="literal">_score</code> 与 <code class="literal">negative_boost</code> 相乘的方式降级后的结果。</p>
<p>为了达到效果， <code class="literal">negative_boost</code> 的值必须小于 <code class="literal">1.0</code> 。在这个示例中，所有包含负向词的文档评分 <code class="literal">_score</code> 都会减半。</p>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="query-scoring.html">« 使用查询结构修改相关度</a>
</span>
<span class="next">
<a href="ignoring-tfidf.html">忽略 TF/IDF »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>